﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JJY.CommonHelper;
using HtmlAgilityPack;
using Lottery.Entity;
using Lottery.DataAccess;

public partial class FootBall_BJSingle_BetCollection : System.Web.UI.Page
{
    const string url = "http://www.bjlot.com/SSMCore.aspx";
    string parmName = "postData=200-re-{0}-{1}-js";
    //string parm = "postData=200-re-2010-01099-js";

    int issueCount = 50; //the biggest issues number in the monthe
    

    protected void Page_Load(object sender, EventArgs e)
    {
        //string str = HttpRequestService.GetHtmlSourceByUrl(url1, parm).Split('^')[0];
        //Response.Write(str);        
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int year = int.Parse(ddlYear.SelectedItem.Text);
        int month = int.Parse(ddlMonth.SelectedItem.Text);

        for (int j = 1; j < issueCount; j++)
        {
            Insert(year, month, j);
        }
    }

    private void Insert(int year, int month, int index)
    {        
        string s = year.ToString().Substring(3,1) + 
            string.Format("{0:00}", month) + string.Format("{0:00}", index);
        string parm = string.Format(parmName,year,s);

        string str = HttpRequestService.GetHtmlSourceByUrl(url, parm).Split('^')[0];

        if (str.Length > 200)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(str);
            HtmlNodeCollection trSet = doc.DocumentNode.SelectNodes("//tr");

            List<ModFootballSingleWDL> modSet = new List<ModFootballSingleWDL>();

            foreach (HtmlNode tr in trSet)
            {
                short homeScore = -1;
                short visitingScore = -1;
                short result = 4;

                HtmlDocument temp = new HtmlDocument();
                temp.LoadHtml(tr.InnerHtml.ToString());
                HtmlNodeCollection tdSet = temp.DocumentNode.SelectNodes("//td");

                ModFootballSingleWDL mod = new ModFootballSingleWDL();
                mod.IssueNumber = int.Parse(year.ToString() + string.Format("{0:00}", month) + string.Format("{0:00}", index));
                mod.IdInIssue = int.Parse(tdSet[0].InnerText);
                mod.MatchTypeName = tdSet[1].InnerText.Replace(" ", string.Empty).Trim();
                mod.HomeTeamName = tdSet[2].InnerText.Replace(" ", string.Empty).Trim();
                mod.VisitingTeamName = tdSet[3].InnerText.Replace(" ", string.Empty).Trim();
                mod.Let = short.Parse(tdSet[4].InnerText);
                if (!short.TryParse(tdSet[5].InnerText.Split(':')[0].ToString(), out homeScore) ||
                    !short.TryParse(tdSet[5].InnerText.Split(':')[1].ToString(), out visitingScore) ||
                    !short.TryParse(tdSet[6].InnerText, out result))
                {
                    //must write this here, or homeSorce will be 0
                    homeScore = -1;
                }
                mod.HomeScore = homeScore;
                mod.VisitingScore = visitingScore;
                mod.Result = result;
                mod.SP = decimal.Parse(tdSet[7].InnerText);
                mod.TotalWinner = decimal.Parse(tdSet[8].InnerText);
                mod.SingleBonuses = decimal.Parse(tdSet[9].InnerText);
                mod.TotalBonuses = decimal.Parse(tdSet[10].InnerText);
                mod.Html = HttpUtility.HtmlEncode(str).Replace("'", "''");

                modSet.Add(mod);
            }

            new LotteryFootballSingleGameWDLController().NewRecordInsert(modSet);
        }
        else
        {
            return;
        }
    }
}
